function maxAreaOfIsland(grid: number[][]): number {
    // 思路，遍历二维数组，先找到一块土地
    // 根据这一块土地延申找周围连接土地，这一步应该用递归，直到找到海水
    // 记录坐标，之后遍历到这个岛的时候直接跳过
    // const coordinate: number[][] = []
    // const area: number[] = [0]
    // grid.map((arr,x) => {
    //     arr.map((item,y) => {
    //         if(item === 1){
    //             // coordinate.push([x,y])
    //             area.push(getArea([x,y],grid))
    //         }
    //     })
    // })
    // // console.log(coordinate)
    // console.log(area)
    // return Math.max(...area)
    // 这之后是DFC算法
    const dfcArr: number[][] = [[0,-1],[0,1],[-1,0],[1,0]]
    const m:number  = grid.length
    const n:number = grid[0].length
    let dfc = (i:number,j:number): number => {
        if (grid?.[i]?.[j]) {
            grid[i][j] = 0
            return dfcArr.reduce((sum,item) => sum + dfc(i+item[0],j+item[1]),1)
        } else{
            return 0
        }
    }
    let area: number = 0
    grid.map((item,x) => {
        item.map((i,y) => {
            if(grid[x][y] === 1){
                area = Math.max(area,dfc(x,y))
            }
        })
    })
    return area
};

// function getArea([x, y]: number[],grid: number[][]): number {
//     if (grid?.[x]?.[y] === 1){
//         grid[x][y] = 0
//         return 1 + getArea([x-1,y],grid) + getArea([x+1,y],grid) + getArea([x,y-1],grid) + getArea([x,y+1],grid)
//     } else {
//         return 0
//     }
// }


const grid :number[][] = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
console.log(maxAreaOfIsland(grid))